﻿var PageNumber = 1;
var IsSearchView = false;
var GroupIDView = 0;
var QuotaComElem = null;
var QuotaComElemVal = null;
var QuotaTimerId = null;
$(document).ready(function () {
    var path = window.location.pathname;
    if (path.indexOf("SetupManageQuotationGroup") != -1) {
        LoadManageQuotationGroupsGrid();
    }
});
$(document).on('change', '.ChangedManageQuotationGroupsElements', function () {
    $('#ManageQuotationGroupDetailsSaveBtn').addClass('BackColorRed');
});
$(document).on('click', '#ManageQuotationGroupMoreBtn', function () {
    PageNumber = PageNumber + 1;
    LoadManageQuotationGroupsGrid();
});
$(document).on('click', '#ManageQuotationGroupSearchBtn', function () {
    IsSearchView = true;
    LoadManageQuotationGroupsGrid();
});
$(document).on('click', '#ManageQuotationGroupDetailsSaveBtn', function () {
    var $form = $('#LoadManageQuotationGroupDetailsForm');
    if ($form.valid()) {
        if ($form[0].length > 0) {
            $.ajax({
                url: $form.attr('action'),
                type: $form.attr('method'),
                data: $form.serialize(),
                beforeSend: function (xhr) {
                    xhr.overrideMimeType('text/plain; charset=x-user-defined');
                },
                success: function (data) {
                    if (data.indexOf("Error") != -1)
                        jAlert(data, "Error");
                },
                error: function (xhr) {
                    jAlert(xhr.statusText, "Error");
                },
                complete: function () {
                    $('#ManageQuotationGroupDetailsSaveBtn').removeClass('BackColorRed')
                    jAlert("Data SuccessFully Saved:", "Successful");
                    LoadManageQuotationGroupsGrid();
                }
            });
        }
        else
            jAlert("You have not data to save:", "Attention!");
    }
});
function LoadManageQuotationGroupsGrid() {
    var SetupManageQuotationGroupsGridViewModel = {
        GroupQuotaIDText: $("#GroupQuotaIDText").val(),
        PageNum: parseInt(PageNumber),
        IsSearch: IsSearchView,
        GroupID: parseInt(GroupIDView),
        SelectedStatusIDText: $("#SelectedStatusIDText").val(),
    };

    var DTO = JSON.stringify(SetupManageQuotationGroupsGridViewModel);
    $.ajax({
        url: urls["SetupLoadManageQuotationGroupsGrid"],
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: DTO,
        success: function (data) {
            $("#SetupManageQuotationGroupsGrid").html(data);
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error");
        },
        complete: function () {
            QuotaGroupStatusDrop();
        }
    });
}
function QuotaGroupStatusDrop() {
    $(".QuotaGroupStatusDrop").each(function (index, elem) {
        $(elem).next().prop("selectedIndex", $(elem).prop("selectedIndex"));
    });
}
$(document).on('change', '.QuotaGroupStatusDrop', function () {
    $(".QuotaGroupStatusDrop").each(function (index, elem) {
        $(elem).next().prop("selectedIndex", $(elem).prop("selectedIndex"));
    });
});